package com.example.play.view

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity.BOTTOM
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.blankj.utilcode.util.ToastUtils
import com.example.common.entity.CommendEntity
import com.example.play.R
import com.example.play.adapter.VideoCommentListAdapter
import com.example.play.databinding.CommentListLayoutBinding
import com.example.play.intent.PlayViewIntent
import com.example.play.state.PlayUiState
import com.example.play.vm.PlayViewModel

import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 *自定义 DialogFragment实现评论列表
 */
@AndroidEntryPoint
class CommentListDialog constructor(private val itemId:String):DialogFragment(){
    private lateinit var mDataBinding: CommentListLayoutBinding
    private val mViewModel :PlayViewModel by activityViewModels()
    private val commentList = mutableListOf<CommendEntity>()
    private lateinit var commentAdapter: VideoCommentListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置样式
        setStyle(STYLE_NO_TITLE, R.style.BottomSheetDialog)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        var dialog =super.onCreateDialog(savedInstanceState)
        dialog.setCanceledOnTouchOutside(true)//设置外部区域可以取消
        dialog.window?.apply {
            setGravity(BOTTOM)
            setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))//背景为透明
        }
        return dialog
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mDataBinding=CommentListLayoutBinding.inflate(inflater,container,false)
        return mDataBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        loadData()
        updateUi()
    }

    private fun initViews() {
        commentAdapter=VideoCommentListAdapter(commentList)
        mDataBinding.expandList.setAdapter(commentAdapter)
    }

    override fun onDestroy() {
        dismiss()
        super.onDestroy()
    }
    override fun onDestroyView() {
        super.onDestroyView()
        dismiss()
    }

    private fun loadData() {
        lifecycleScope.launch {
            mViewModel.channel.send(PlayViewIntent.LoadVideoCommends(0,itemId))
        }
    }

    private fun updateUi() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED){
                mViewModel.uiStateFlow.collectLatest {
                    when(it){
                        is PlayUiState.LoadVideoCommendsSuccess -> showVideoCommendList(it.list)
                        is PlayUiState.LoadVideoCommendsFail -> ToastUtils.showLong(it.msg)
                        else ->{}
                    }
                }
            }
        }
    }

    private fun showVideoCommendList(list: List<CommendEntity>) {
        commentList.clear()
        commentList.addAll(list)
        commentAdapter.notifyDataSetChanged()
    }
}